package com.whfc.mach.manager.util;

import com.whfc.common.util.DateUtil;
import com.whfc.common.util.MathUtil;
import com.whfc.mach.enums.NetState;
import com.whfc.mach.enums.WorkState;

import java.util.Date;

/**
 * @Description: 设备工具类
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/12/16 17:13
 */
public class MachUtil {

    /**
     * 接收硬件的历史数据,时间范围是7天
     */
    private static final int DEVICE_HISTORY_DAYS = 7;

    /**
     * 接收硬件数据,正常范围不超过服务器时间10分钟.
     */
    private static final int DEVICE_EXCEED_MINUTES = 10;

    /**
     * 计算时长
     *
     * @param startTime
     * @param endTime
     * @return 时长(单位 : 秒)
     */
    public static int calculateTime(Date startTime, Date endTime) {
        int times = (int) ((endTime.getTime() - startTime.getTime()) / 1000);
        return times;
    }

    /**
     * 计算设备开工率
     *
     * @param workMachNum
     * @param totalMachNum
     * @return
     */
    public static Double computeOperatingRate(Integer workMachNum, Integer totalMachNum) {
        double operatingRate = 0D;
        if (totalMachNum > 0) {
            operatingRate = MathUtil.round(100 * (double) workMachNum / (double) totalMachNum, 1);
        }
        return operatingRate;
    }

    /**
     * 是否停机状态
     *
     * @param workState
     * @return
     */
    public static boolean isPowerOff(Integer workState) {
        return WorkState.STOP.getValue().equals(workState) || WorkState.SLEEP.getValue().equals(workState);
    }

    /**
     * 是否开机状态
     *
     * @param workState
     * @return
     */
    public static boolean isPowerOn(Integer workState) {
        return WorkState.IDLE.getValue().equals(workState) || WorkState.RUN.getValue().equals(workState);
    }

    /**
     * 是否相同
     * 1.工作状态相同
     * 2.停机或休眠是相同的状态
     *
     * @param workState1
     * @param workState2
     * @return
     */
    public static boolean isSameState(Integer workState1, Integer workState2) {
        return workState1 != null && workState2 != null &&
                (workState1.equals(workState2) || (isPowerOff(workState1) && isPowerOff(workState2)));
    }

    /**
     * 是否怠机状态
     *
     * @param workState
     * @return
     */
    public static boolean isIdleState(Integer workState) {
        return WorkState.IDLE.getValue().equals(workState);
    }

    /**
     * 是否工作状态
     *
     * @param workState
     * @return
     */
    public static boolean isRunState(Integer workState) {
        return WorkState.RUN.getValue().equals(workState);
    }

    /**
     * 是否休眠状态
     *
     * @param workState
     * @return
     */
    public static boolean isSleepState(Integer workState) {
        return WorkState.SLEEP.getValue().equals(workState);
    }

    /**
     * 是否静止状态
     *
     * @param workState
     * @return
     */
    public static boolean isStopState(Integer workState) {
        return WorkState.STOP.getValue().equals(workState);
    }

    /**
     * 是否是离线
     *
     * @param netState
     * @return
     */
    public static boolean isOfflineState(Integer netState) {
        return NetState.OFFLINE.getValue().equals(netState);
    }

    /**
     * 是否有效状态
     *
     * @param workState
     * @return
     */
    public static boolean isValidState(Integer workState) {
        return isPowerOn(workState) || isPowerOff(workState);
    }

    /**
     * 是否无效状态
     *
     * @param workState
     * @return
     */
    public static boolean isInvalidState(Integer workState) {
        return !isValidState(workState);
    }

    /**
     * 是否有效的硬件时间
     *
     * @param logTime
     * @param serverTime
     * @return
     */
    public static boolean isValidDataTime(Date logTime, Date serverTime) {
        Date serverDate = DateUtil.getDate(serverTime);
        Date maxTime = DateUtil.addMinutes(serverTime, DEVICE_EXCEED_MINUTES);
        Date minTime = DateUtil.addDays(serverDate, -DEVICE_HISTORY_DAYS);
        return logTime.after(minTime) && logTime.before(maxTime);
    }

    /**
     * 是否历史数据时间
     *
     * @param logTime
     * @param latestLogTime
     * @return
     */
    public static boolean isHistoryDataTime(Date logTime, Date latestLogTime) {
        return latestLogTime != null && logTime.before(latestLogTime);
    }
}
